package cn.yinyu.queue.module.ecg.dal.mysql.queuesequence;

import java.util.*;

import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.yinyu.queue.framework.mybatis.core.mapper.BaseMapperX;
import cn.yinyu.queue.module.ecg.dal.dataobject.queuesequence.QueueSequenceDO;
import org.apache.ibatis.annotations.*;
import cn.yinyu.queue.module.ecg.controller.admin.queuesequence.vo.*;

/**
 * 当天序号 Mapper
 *
 * @author 金华医院
 */
@Mapper
public interface QueueSequenceMapper extends BaseMapperX<QueueSequenceDO> {

    default PageResult<QueueSequenceDO> selectPage(QueueSequencePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<QueueSequenceDO>()
                .eqIfPresent(QueueSequenceDO::getCheckType, reqVO.getCheckType())
                .eqIfPresent(QueueSequenceDO::getTimeSlot, reqVO.getTimeSlot())
                .eqIfPresent(QueueSequenceDO::getQueueNo, reqVO.getQueueNo())
                .eqIfPresent(QueueSequenceDO::getQueueVipNo, reqVO.getQueueVipNo())
                .eqIfPresent(QueueSequenceDO::getQueueFull, reqVO.getQueueFull())
                .eqIfPresent(QueueSequenceDO::getQueueVipFull, reqVO.getQueueVipFull())
                .betweenIfPresent(QueueSequenceDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(QueueSequenceDO::getId));
    }

    @Select(" select * from yy_queue.queue_sequence where check_type=#{checkType}; ")
    List<QueueSequenceDO> selectTimeslotByCheckType(Integer checkType);

    @Select( "select count(1) from yy_queue.queue_sequence")
    Integer getQueueSequenceTableRowCount();

    @Delete("delete from yy_queue.queue_sequence where TO_DAYS(create_time) != TO_DAYS(NOW())")
    void clearQueueSequenceTableNotCurrent();

    @Update("truncate table yy_queue.queue_sequence")
    void clearQueueSequenceTable();

    @Select("select queue_no from yy_queue.queue_sequence where check_type=#{checkType} and time_slot=#{timeslot} and queue_no < queue_full for update; ")
    Integer selectQueueNoForUpdate(@Param("checkType") Integer checkType, @Param("timeslot") Integer timeslot);

    @Update("update yy_queue.queue_sequence set queue_no = queue_no + 1 where check_type=#{checkType} and time_slot=#{timeslot} and queue_no=#{curQueueNo} and queue_no < queue_full; ")
    Integer updateGivenCheckTypeTimeslotSeqNo(@Param("checkType") Integer checkType, @Param("timeslot") Integer timeslot, @Param("curQueueNo") Integer curQueueNo);

    @Select("select queue_vip_no from yy_queue.queue_sequence where check_type=#{checkType} and time_slot=#{timeslot} and queue_no < queue_full for update; ")
    Integer selectQueueVipNoForUpdate(@Param("checkType") Integer checkType, @Param("timeslot") Integer timeslot);

    @Update("update yy_queue.queue_sequence set queue_vip_no = queue_vip_no + 1 where check_type=#{checkType} and time_slot=#{timeslot} and queue_vip_no=#{curQueueVipNo} and queue_vip_no < queue_vip_full; ")
    Integer updateGivenCheckTypeTimeslotVipSeqNo(@Param("checkType") Integer checkType, @Param("timeslot") Integer timeslot, @Param("curQueueVipNo") Integer curQueueVipNo);
}
